package com.bootx.job;

import com.bootx.entity.Article;
import com.bootx.service.ArticleService;
import com.bootx.util.ai100.AiTop100Utils;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author black
 */
@Component
public class Ai100Job {

    @Resource
    private ArticleService articleService;
    @Resource
    private JdbcTemplate jdbcTemplate;

    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    // @Scheduled(fixedRate = 1000*60*60*24)
    public void getCategory(){
        int lastIndex = 28275+1000;
        for(int i = 0; i < 100000; i++) {
            List<Article> articleList = AiTop100Utils.getArticleList(lastIndex);
            for (Article article : articleList) {
                System.out.println(article.getUrl());
                article.setContent(AiTop100Utils.getArticleDetail(article.getUrl()));
                try {
                    articleService.save(article);
                }catch (Exception e){
                    e.printStackTrace();
                }
                int min = Integer.parseInt(article.getOtherId().replace("aitop100_", ""));
                lastIndex = Math.min(min, lastIndex);
            }
        }
    }

    @Scheduled(fixedRate = 1000*60*60*3)
    public void getAllArticle(){

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        int lastIndex = 400;
        try {
            lastIndex = jdbcTemplate.queryForObject("select max(X.id) from (select (REPLACE(otherId,'ai100_','')+0) `id` from article) AS X",Integer.class);
        }catch (Exception ignored){

        }
        for(int i = lastIndex+1; i < lastIndex+500; i++) {
            Integer i1 = jdbcTemplate.queryForObject("select count(id) from article where otherId='ai100_" + i + "'", Integer.class);
            if(i1>0){
                System.out.println(i +":存在");
                continue;
            }
            int finalI1 = i;
            executorService.submit(()->{
                try {
                    long start = System.currentTimeMillis();
                    Article article = AiTop100Utils.getArticle("https://www.aitop100.cn/infomation/details/" + finalI1 + ".html");
                    if(article!=null) {
                        System.out.println(finalI1 +"采集时间："+(System.currentTimeMillis()-start)+"============================");
                        try {
                            articleService.create(article);
                            System.out.println(finalI1 +"插入时间："+(System.currentTimeMillis()-start));
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }catch (Exception ignored){

                }
            });


        }
    }
}